package com.example2016.cjj.mycustomviewdemo.MyView;

import android.content.Context;
import android.support.v4.view.ViewConfigurationCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by CJJ on 2017/3/9.
 */

public class CustomListView extends ViewGroup {

    private static final String TAG = "CustomListView";

    //滑动与点击的临界点值,超过这个值就是滑动，否则就是点击。
    private int mTouchSlop;


    int mChildCount =15;


    public CustomListView(Context context) {
        this(context,null);
    }

    public CustomListView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //获取TouchSlop值
        ViewConfiguration configuration = ViewConfiguration.get(context);
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);

    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        int childCount = getChildCount();
//        for(int i=0; i< childCount; i++){
//            View childView = getChildAt(i);
//            //测量每一个子控件的大小
//            measureChild(childView,widthMeasureSpec,heightMeasureSpec);
//        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if(changed){

           // int childCount =15;
            for(int i=0; i< mChildCount; i++){
                TextView childView = new TextView(getContext());
                childView.setText("textview"+i);
                LayoutParams lp = new MarginLayoutParams(LayoutParams.MATCH_PARENT,100);
                addViewInLayout(childView,i,lp);  //添加子view

                childView.layout(0,i*100
                               ,getMeasuredWidth(),(i+1)*100);

            }



//            int childCount =getChildCount();
//            for(int i=0; i< childCount; i++){
//                View childView = getChildAt(i);
//                //把每一个子控件在垂直方向上布局
//                childView.layout(0,i*childView.getMeasuredHeight()
//                                ,childView.getMeasuredWidth(),(i+1)*childView.getMeasuredHeight());
//
//            }

        }

    }


    float mYDown;
    float mYMove;
    boolean interceptFlag= false;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                mYDown = ev.getY();

                interceptFlag= false;
                break;
            case MotionEvent.ACTION_MOVE:
                mYMove = ev.getY();
                float diffY = Math.abs(mYMove - mYDown);
                //当手指拖动值大于TouchSlop时，认为应该是进行滚动，拦截子控件的事件
                if( diffY > mTouchSlop){
                    interceptFlag = true;
                }

                mYDown = mYMove;
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                interceptFlag= false;
                break;
        }

        return interceptFlag;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

//        if(mVelocityTracker== null){
//            mVelocityTracker = VelocityTracker.obtain();    //获取速率追踪器实例
//        }
//        mVelocityTracker.addMovement(event);


        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
//                //如果上一次滚动动画还没结束，手指又按下了，那么我们结束上一次动画。
//                if(mScroller !=null){
//                    if( !mScroller.isFinished() ){
//                        mScroller.abortAnimation();
//                    }
//                }
                mYDown = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                mYMove = event.getY();
                int scrolleY = (int) (mYDown - mYMove);

                offsetChildrenTopAndBottom(-scrolleY);


                mYDown = mYMove;
                break;
            case MotionEvent.ACTION_UP:
//                final VelocityTracker velocityTracker = mVelocityTracker;
//                //计算速率
//                velocityTracker.computeCurrentVelocity(1000);
//                //获取x轴的速率值,向左滑是负值，向右滑是正值
//                float velocityX =velocityTracker.getXVelocity();
//
//                //判断速率是否超过了定义的最小速率，如果超过了，那就滑到上\下一页，
//                // 否则就正常滑动（判断是否过了宽度的一半，如果超过一般,那就滑到上\下一页）
//                if( Math.abs( velocityX ) > SNAP_VELOCITY){
//                    if( velocityX <0){
//                        //左滑
//                        SnapToScreen(curScreen +1);
//                    }else  if( velocityX > 0){
//                        //右滑
//                        SnapToScreen(curScreen -1);
//                    }
//
//                }else {
//                    WidthToScreen();
//                }
                break;
        }


        return true;
    }



    public void offsetChildrenTopAndBottom(int offset) {

        for(int i=0; i< getChildCount(); i++){
            View childView = getChildAt(i);
            childView.offsetTopAndBottom(offset);

//            int cbottom  = childView.getBottom();
//
//            if( cbottom < 0 ){
//                Log.i(TAG, "offsetChildrenTopAndBottom:child"+i + ":回收");
//                detachViewFromParent(i);
//            }
//
//            int ctop = childView.getTop();
//            if( ctop > getMeasuredHeight()){
//                Log.i(TAG, "offsetChildrenTopAndBottom:child"+i + ":在屏幕外");
//            }

           // Log.i(TAG, "offsetChildrenTopAndBottom: child"+i+":top="+childView.getTop() +",bottom="+childView.getBottom());

        }


        View lastview = getChildAt(mChildCount-1);

        Log.i(TAG, "offsetChildrenTopAndBottom: lastview.getBottom()="+lastview.getBottom());

        if( lastview.getBottom() <= getMeasuredHeight() ){

            TextView childView = new TextView(getContext());
            childView.setText("textview" + mChildCount );
            LayoutParams lp = new MarginLayoutParams(LayoutParams.MATCH_PARENT,100);
            //addViewInLayout(childView,mChildCount-1,lp);  //添加子view

            attachViewToParent(childView,mChildCount-1,lp);
            childView.layout(0,mChildCount*100
                    ,getMeasuredWidth(),(mChildCount+1)*100);


            Log.i(TAG, "offsetChildrenTopAndBottom: 后面移入一个"+mChildCount);

            mChildCount ++;
        }


        invalidate();

    }




}
